﻿using System;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace HYRElectric.MEPSpace
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdUpdateSpaceInfo : YJKCommandAuto
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				FamilyLibDatabase.GetSqliteDB(SubjectType.System, cmdData.Application.Application.VersionNumber);
				Document document = cmdData.Application.ActiveUIDocument.Document;
                Space space = document.GetElementById(cmdData.Application.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new Assist.SpaceFilter()).ElementId) as Space;
				this.UpdateSpaceInfo(space);
				MessageBox.Show("空间信息更新成功。");
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		private void UpdateSpaceInfo(Space space)
		{
			Document document = space.Document;
			SubTransaction subTransaction = new SubTransaction(document);
			subTransaction.Start();
			try
			{
				Room room = space.Room;
				Parameter parameter = room.get_Parameter(BuiltInParameter.ROOM_DEPARTMENT);
				Parameter parameter2 = room.get_Parameter(BuiltInParameter.ROOM_NAME);
				Parameter parameter3 = room.get_Parameter(BuiltInParameter.ROOM_NUMBER);
				Parameter parameter4 = room.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
				string text = string.Format("房间名称:{0},房间编号:{1}部门:{2}房间注释:{3}", new object[]
				{
					parameter2.AsString(),
					parameter3.AsString(),
					parameter.AsString(),
					parameter4.AsString()
				});
				Parameter parameter5 = space.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
				parameter5.Set(text);
				subTransaction.Commit();
			}
			catch
			{
				subTransaction.RollBack();
			}
		}
	}
}
